package org.naxx.security.datatype.impl;

import org.apache.log4j.Logger;
import org.naxx.security.common.NotYetImplemented;
import org.naxx.security.datatype.DataType;
import org.naxx.security.datatype.DataTypeValueResolver;

/**
 * TODO implement this to full, especially handling of dates/times
 */
public final class DataTypeValueResolverImpl implements DataTypeValueResolver {

	private static final Logger logger = Logger.getLogger(DataTypeValueResolverImpl.class);
	
	public Object resolveValue(DataType dataType, String source) {
		source = source.trim();
		if (logger.isDebugEnabled()) {
			logger.debug("Resolving '" + source + "' into type '" + dataType
					+ "'");
		}		
		if(dataType.getUri().equals("http://www.w3.org/2001/XMLSchema#string")) {
			return source;
		}
		else if(dataType.getUri().equals("http://www.w3.org/2001/XMLSchema#integer")) {
			return Integer.valueOf(source);
		}
		else if(dataType.getUri().equals("http://www.w3.org/2001/XMLSchema#double")) {
			return Double.valueOf(source);
		}
		else if(dataType.getUri().equals("http://www.w3.org/2001/XMLSchema#boolean")) {
			return Boolean.valueOf(source);
		}
		else if(dataType.getUri().equals("http://www.w3.org/2001/XMLSchema#anyURI")) {
			return source;
		}
		else if(dataType.getUri().equals("http://www.w3.org/2001/XMLSchema#string")) {
			return source;
		}
		else if(dataType.getUri().equals("http://www.w3.org/2001/XMLSchema#date")) {
			return source;
		}
		else if(dataType.getUri().equals("http://www.w3.org/2001/XMLSchema#time")) {
			return source;
		}
		else if(dataType.getUri().equals("http://www.w3.org/2001/XMLSchema#dateTime")) {
			return source;
		}
		else if(dataType.getUri().equals("http://www.w3.org/2001/XMLSchema#hexBinary")) {
			return source;
		}
		else if(dataType.getUri().equals("http://www.w3.org/2001/XMLSchema#base64Binary")) {
			return source;
		}
		else if(dataType.getUri().equals("http://www.w3.org/TR/2002/WD-xquery-operators-20020816#dayTimeDuration")) {
			return source;
		}
		else if(dataType.getUri().equals("http://www.w3.org/TR/2002/WD-xquery-operators-20020816#yearMonthDuration")) {
			return source;
		}
		else if(dataType.getUri().equals("urn:oasis:names:tc:xacml:1.0:data-type:x500Name")) {
			return source;
		}
		else if(dataType.getUri().equals("urn:oasis:names:tc:xacml:1.0:data-type:rfc822Name")) {
			return source;
		}
		else if(dataType.getUri().equals("urn:oasis:names:tc:xacml:2.0:data-type:ipAddress")) {
			return source;
		}
		else if(dataType.getUri().equals("urn:oasis:names:tc:xacml:2.0:data-type:dnsName")) {
			return source;
		}
		else {
			throw new NotYetImplemented("Type resolution of '" + dataType + "' not yet implemented");
		}
	}
}
